<?
class Process extends Root{
	var $TempVar;
	var $test;
	var $Skin;
	var $BlockList;
	var $Beacons;
	var $PostBeacons;

// --------------------------------------------------//
//			CONSTRUCTEUR		     //
// --------------------------------------------------//
	function Process($Type="") {
// 		$this->initPostBeacons();
		$this->PostBeacons=$GLOBALS["Systeme"]->Conf->get("PROCESS::POST::BEACON");
		switch ($Type) {
			case "Skin":
// 				$this->initSkinsBeacons();
				$this->Beacons=$GLOBALS["Systeme"]->Conf->get("PROCESS::SKIN::BEACON");
			break;
			default:
			case "Struct":
// 				$this->initAllBeacons();
				$this->Beacons=$GLOBALS["Systeme"]->Conf->get("PROCESS::ALL::BEACON");
			break;
		}
		$this->TempVar = $GLOBALS["Systeme"]->RegVars;
	}


// --------------------------------------------------//
//			GESTION DES VARIABLES	     //
// --------------------------------------------------//
	//enregistrement d'une variable temporaire
	function RegisterTempVar($Name,$Var) {
//  		echo "-> $Name \r\n";
// 		print_r($Var);
		if (is_string($Name))if (sizeof(explode("::",$Name))>1){
			$Res = explode("::",$Name);
			if(is_object($this->TempVar[$Res[0]])){
				$N = $Res[1];
				$this->TempVar[$Res[0]]->$N = $Var;
// 				echo "OBJET ".$Res[0]."=>".$Res[1]." = $Var\r\n";
			}else{
				$this->TempVar[$Res[0]][$Res[1]] = $Var;
// 				echo "TABLEAU ".$Res[0]."=>".$Res[1]." = $Var\r\n";
			}
		}else{
			 $this->TempVar[$Name] = $Var;
// 			echo "STANDARD ".$Name."=>".$Var."\r\n";
		}
//  		echo "--------------------\r\n";
	}

	function getTempVar($Name){
		return $this->TempVar[$Name];
	}
	//traitement d'une chaine de caractere de maniere recursive
	//Cette fonction a pour but d'optimiser le traitement si il n y a  pas de variable dans la chaine
	//alors on arrette le proc
	function processingVars($Data) {
		if (!is_string($Data)) return $Data;
		if (sizeof(explode("[!",$Data))>1) {
 			$Data = $this->searchAndReplaceVars($Data);
		}
		return $Data;
	}

	function processVars($Data,$test=""){
// 		if ($Data=="Lien") print_r($this->TempVar);
		if (sizeof(explode("[!",$Data))>1) {
			//On envoie dans la moulinette
//   			echo "\r\n-----DEPART-->".$Data."\r\n";
			$Data = $this->searchAndReplaceVars($Data);
//     			echo "\r\n-----RETOUR-->".$Data."\r\n";
		}
		$sData = $Data;
// 		echo $Data."\r\n";
		if (!is_string($Data))return $Data;
		//On detecte l existence d un calcul
		if (preg_match("#(.*)(\:\=|\=\=|\:\+|\:\*|\:\/(?!\/)|\:\-|\*\=|\-\=|\+\=|\.\=|\/\=|\%\=)(.*)#s",$Data,$Out)){
			//On extrait les valeurs
			$Operateur = $Out[2];
			$Val1 = $Out[1];
			if ($Val1=="") $Val1 = $Out[1];
			$Val2 = $Out[3];
			if ($Val2=="") $Val2 = $Out[3];
			if ($Val2=="EOL")$Val2="\n";
			switch($Operateur){
				case "==":
// 					echo $Val1."=".$Val2."\r\n";
					//Affectatuion forc�e
					if (strlen($Out[1])){
						$this->RegisterTempVar($Out[1],$this->searchAndReplaceVars($Out[3]));
					}
					$Data="";
				break;
				case ":=":
					//Affectation par defaut
					if (strlen($Out[1])&&($this->ProcessPostVars($Out[1])==""||$this->ProcessPostVars($Out[1])==$Out[1])){
						if (preg_match("#(.*)(\:\=|\=\=|\:\+|\:\*|\:\/|\:\-|\*\=|\/\=)(.*)#s",$Out[3],$Out2)) $Out[3]=$this->TempVar[$Out[3]];
						//On verifie qu il ne s agissent pas d un tableau
						if (sizeof($Tab = split ('::',$Out[1]))>1) {
							//C un tableau
							$Temp = $this->ProcessVars($Tab[0]);
// 							echo "Affectation d un tableau $Data\r\n";
// 							print_r($Temp);
// 							echo " - ".$Out[3]." - Separateur ".$Tab[1]."\r\n";
							if ($Tab[1]!="")$Temp[$Tab[1]] = $Out[3];
							else $Temp[] = $Out[3];
// 							print_r($Temp);
// 							echo "-------------------------\r\n";
							$this->RegisterTempVar($Tab[0],$Temp);
						}else{
// 		 					echo $Out[1]."=".$Out[3]."\r\n";
							if (sizeof(explode("[!",$Out[3],2))>1){
								$Out[3] = $this->processVars(preg_replace("#^\[\!(.*)\!\]$#","$1",$Out[3]));
							}
							$this->RegisterTempVar($Out[1],$Out[3]);
						}
					}
					$Data="";
				break;
				case ":/":
					if (!is_numeric($Val1)&&!is_numeric($Val2)) {
						//Alors il s agit d'une division de chaine
						if ($Val2=="%RC%"){
							$Data=explode("\r\n",trim($Val1));	
						}else {
							//Alors il s agit d'une division de chaine
						  //(fred)Ajout pour division par espace
						  if ($Val2=="") $Val2=" ";
								$Data=explode($Val2,$Val1);
	// 						print_r($this->TempVar[$Out[1]]);
						}
					}else{
						if (!is_numeric($Val1)) {
							$Val1=$this->processVars($Val1);
						}
						if ($Val2!=""&&$Val2!=0&&!is_array($Val1)) $Data = $Val1/$Val2;
						else $Data=Array($Val1);
					}
				break;
				case ":%":
					if (!is_numeric($Val1)) $Val1=$this->processVars($Val1);
					if ($Val2!=""&&$Val2!=0) $Data = $Val1%$Val2;
					else $Data=0;
				break;
				case ":+":
					if (!is_numeric($Val1)) $Val1=$this->processVars($Val1);
					$Data = $Val1+$Val2;
				break;
				case ":*":
					if (!is_numeric($Val1)) $Val1=$this->processVars($Val1);
					$Data = $Val1*$Val2;
//  					echo "Multiplication ".$Out[1]."|".$Out[3]."|".$this->processVars($Val1)."*".$Val2."=".$Data."</BR>";
				break;
				case ":-":
					if (!is_numeric($Val1)) $Val1=$this->processVars($Val1);
					$Data = $Val1-$Val2;
//  					echo "Soustraction ".$Out[1]."|".$Out[3]."|".$Val1."-".$Val2."=".$Data."</BR>";
				break;
				case "*=":
					$Temp = $this->processVars($Val1);
					$this->TempVar[$Out[1]]=$Val2*$Temp;
					$Data="";
				break;
				case "/=":
					if (!is_numeric($Val1)&&!is_numeric($Val2)) {
						if ($Val2=="%RC%"){
							$this->TempVar[$Out[1]]=explode("\r\n",trim($this->processVars($Val1)));	
						}else {
							//Alors il s agit d'une division de chaine
							 $this->TempVar[$Out[1]]=explode($Val2,$Val1);
	// 						print_r($this->TempVar[$Out[1]]);
						}
					}else{
						if (!is_numeric($Val1)) {
							$Val1=$this->processVars($Val1);
						}
						if ($Val2!=""&&$Val2!=0) $this->TempVar[$Out[1]]=$Val1/$Val2;
					}
					$Data="";
				break;
				case "%=":
					$Temp = $this->processVars($Val1);
					$this->TempVar[$Out[1]]=$Temp%$Val2;
					$Data="";
				break;
				case "+=":
					if (!is_numeric($Val1)&&!is_numeric($Val2)) {
						//Alors il s agit dune concatenation de chaine
						$Val1=$this->processVars($Val1);
						$this->TempVar[$Out[1]]=$Val1.$Val2;
					}else{
						$Temp = $this->processVars($Out[1]);
						$this->RegisterTempVar($Out[1],$Val2+$Temp);
					}
					$Data="";
				break;
				case "-=":
					$Temp = $this->TempVar[$Out[1]];
					$this->TempVar[$Out[1]]=$Temp-$Val2;
					$Data="";
				break;
				case ".=":
					$Temp = $this->TempVar[$Out[1]];
					$this->TempVar[$Out[1]]=$Temp.$Val2;
					$Data="";
				break;
			}
			return $Data;
		}
		//On detecte l existence d un split
		if (preg_match("#(.*)(\:\/|\/\=)(.*)#",$Data,$Out)){
//
		}else{
// 			echo "Ne correspond pas ".$Data."\r\n";
		}

		$Tab = split ('::',$Data);
// 		print_r($Tab);
		if (!$this->test){
			$this->test=1;
		}
		//Si split a trouve une suite de ::, alors on sait quil faut faire des recherches
		if (sizeof($Tab)>1) {
			switch ($Tab[0]) {
				case "JSON":
					$Json=$GLOBALS['Systeme']->RegVars['JSON'];
					$chaine='$GLOBALS["Systeme"]->RegVars["JSON"]->'.$Tab[1].'->'.$Tab[2];
					$Data = eval("return ".$chaine.";");
				break;
				case "CONF":
					$Data=preg_replace("#CONF\:\:#","",$Data);
					$Data=$GLOBALS['Systeme']->Conf->get($Data);
				break;
				case "SKIN":
					$Data=preg_replace("#SKIN\:\:#","",$Data);
					$Data=$GLOBALS['Systeme']->SkinObj->get($Data);
				break;
				case "TMS":
					if ($Tab[1]=="Now") $Data=time();
					else if ($Tab[1]=="ThisMonth") {$Data=mktime(0,0,0,0,1,0);}
				break;
				case "DEBUG":
                                        $Tab = split ('::',$Data);
					$Data=preg_replace("#DEBUG\:\:#","",$Data);
					echo "*** DEBUG ".$Data." MODE ***\r\n";
					print_r($this->processVars($Data));
					$Data="";
				break;
				case "Module":
					//Si le premier mot est Module, alors on recherche des informations dans le module.
					$Function = explode("|",$Tab[count($Tab)-1]);
					$Tab[count($Tab)-1] = $Function[0];
					if ($Tab[1]=="Actuel"){
						//On pointe sur le module en cours.
/*						echo "---> ".$GLOBALS["Systeme"]->RegVars["Lien"]."\r\n";
						$temp = explode("/",$GLOBALS["Systeme"]->RegVars["Lien"]);
						$temp = explode (".",$temp[0]);*/
						$BonModule = $GLOBALS["Systeme"]->CurrentModule;
					}else{
						//On pointe sur un module en particulier
						foreach ($GLOBALS['Systeme']->Modules as $Module){
							if ($Tab[1]==$Module->Nom){
								$BonModule = $Module->Nom;
							}
						}
					}
					//On verifie qu il n y pas d erreur dans la denomination du module
					if ($BonModule=="") return "Erreur Module inexistant";
					$Obj=$chaine="\$GLOBALS['Systeme']->Modules['".$BonModule."']";
					//On initialise le module et on charge le schema
					if (is_object(eval("return ".$Obj.";"))) $TmpObj = eval($Obj."->loadSchema();");
					//On construit la requete en syntaxe objet
					for ($i=2;$i<count($Tab);$i++){
						if ($i<count($Tab)-1) $Obj.='->'.$Tab[$i];
						$chaine.='->'.$Tab[$i];
					}
					//On recupere la liste des proprietes de l objet demande
					$TmpObj = eval("return ".$Obj.";");
					//ON verifie qu il s agisse bien d un objet
					if (is_object($TmpObj)){
						//On teste si il s agit d une propriete ou d une methode
						$Temp = get_object_vars(eval("return ".$Obj.";"));
						if (array_key_exists($Tab[count($Tab)-1],$Temp)) {
							//C est une propriete
							$Data = eval("return ".$chaine.";");
						}else{
							//Sinon verification de l existence de la methode
							if (method_exists(eval("return $Obj;"),$Tab[count($Tab)-1])){
								//Si il y a des parametres alors on les ajoutent
								if (count($Function)>1){
									$chaine = $chaine."(".$Function[1].")";
								}else{
									$chaine = $chaine."()";
								}
								$Data=eval("return $chaine;");
							}
						}
					}
				break;
				case "Systeme":
					$Function = explode("|",$Tab[count($Tab)-1]);
					$Tab[count($Tab)-1] = $Function[0];
					$Obj=$chaine="\$GLOBALS['Systeme']";
					for ($i=1;$i<count($Tab);$i++){
						if ($i<count($Tab)-1) $Obj.='->'.$Tab[$i];
						$chaine.='->'.$Tab[$i];
					}
					//Verification de l existence de la propriete
					//On recupere la liste des proprietes de l objet demande
					$Temp = get_object_vars(eval("return ".$Obj.";"));
					if (is_array($Temp)&&array_key_exists($Tab[count($Tab)-1],$Temp)) {
						$Data = eval("return ".$chaine.";");
					}else{
						//Sinon verification de l existence de la methode
						if (method_exists(eval("return $Obj;"),$Tab[count($Tab)-1])){
							if (count($Function)>1){
								$chaine = $chaine."(".$Function[1].")";
							}else{
								$chaine = $chaine."()";
							}
							$Data=eval("return $chaine;");
						}
					}
					//Sinon Erreur

				break;
				case "ObjectClass":
					//Alors on genere un genericClassVide et on l interroge
					$Data = new genericClass($Tab[1]);
				break;

				case "Vars":
					switch ($Tab[1]) {
						case "Post":
							$Data="";
							if (sizeof($GLOBALS["Systeme"]->PostVars)) {
								foreach ($GLOBALS["Systeme"]->PostVars as $Key=>$Value) {
									$Temp["Value"] = $Value;
									$Temp["Name"] = $Key;
									$Data[] = $Temp;
								}
							}
						break;
						case "Files":
							$Data="";
							if (sizeof($_FILES)) {
								foreach ($_FILES as $Key=>$Value) {
									$Data[$Key] = $Value;
								}
							}
						break;
						case "Get":
							$Data="";
							if (sizeof($GLOBALS["Systeme"]->GetVars)) {
								foreach ($GLOBALS["Systeme"]->GetVars as $Key=>$Value) {
									$Temp["Value"] = $Value;
									$Temp["Name"] = $Key;
									$Data[] = $Temp;
								}
							}
						break;
						case "Temp":
							$Data="";
							if (sizeof($GLOBALS["Systeme"]->TempVars)) {
								foreach ($GLOBALS["Systeme"]->TempVars as $Key=>$Value) {
									$Temp["Value"] = $Value;
									$Temp["Name"] = $Key;
									$Data[] = $Temp;
								}
							}
						break;
						case "Global":
							print_r($GLOBALS["Systeme"]->RegVars);
							$Data="";
							if (sizeof($GLOBALS["Systeme"]->RegVars)) {
								foreach ($GLOBALS["Systeme"]->RegVars as $Key=>$Value) {
									$Temp["Value"] = $Value;
									$Temp["Name"] = $Key;
									$Data[] = $Temp;
								}
							}
						break;
						case "Process":
							print_r($this);
						break;
						case "Session":
							print_r($_SESSION);
						break;
					}
				break;
				case "SERVER":
					return $_SERVER[$Tab[1]];
				break;
				case "SHELL":
					return shell_exec($Tab[1]);
				break;
				case "Math":
					preg_match("#(.*)\((.*)\)#",$Tab[1],$Out);
					$Params = explode(",",$Out[2]);
					switch ($Out[1]){
						case "Round":
							return round($Params[0]);
						break;
						case "Floor":
							return floor($Params[0]);
						break;
						case "Price":
							return round($Params[0]*100)/100;
						break;
					}
				break;
				case "Date":
					preg_match("#(.*)\((.*)\)#",$Tab[1],$Out);
					$Params = explode(",",$Out[2]);
					switch ($Out[1]){
						case "getYear":
							return date("Y",mktime());
						break;
						case "getMonth":
							return date("m",mktime());
						break;
						case "getTime":
							return date("H:i:s",$Params[0]);
						break;
					        case "getDayOfWeek":
                                                        $j = array("Mon"=>"Lundi","Tue"=>"Mardi","Wed"=>"Mercredi","Thu"=>"Jeudi","Fri"=>"Vendredi","Sat"=>"Samedi","Sun"=>"Dimanche");
                                                        return $j[date("D",(($Params[0]!="")?$Params[0]:time()))];
                                               break;
                                               case "getDaysOfWeek":
                                                        $j = array("Mon"=>"Lundi","Tue"=>"Mardi","Wed"=>"Mercredi","Thu"=>"Jeudi","Fri"=>"Vendredi","Sat"=>"Samedi","Sun"=>"Dimanche");
                                                        return $j;
                                               break;
					       case "getMonthsOfYear":
						 return array("Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre");
					       break; 
                                               case "getMonthAlpha":
                                                        $lu = array("1"=>"Janvier","2"=>"Fevrier","3"=>"4","Apr"=>"Avril","5"=>"May","6"=>"Juin","7"=>"Juillet","8"=>"Aout","9"=>"Septembre","10"=>"Octobre","11"=>"Novembre","12"=>"Decembre");
                                                        return $lu[date("n",(($Params[0]!="")?$Params[0]:time()))];
                                               break;
					       case "getWeekDayNum":
						 $d = array("Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi","Dimanche");
						 return array_search($Params[0],$d);
						case "getDay":
							return date("d",mktime());
						break;
						case "Floor":
							return floor($Params[0]);
						break;
						case "Price":
							return round($Params[0]*100)/100;
						break;
						case "getDate":
							return date($Params[0],$Params[1]);
						break;
					}
				break;
				case "Utils":
					//preg_match("#(.*)\((.*)\)#",$Tab[1],$Out);
                                        $Tab = split ('::',$Data,2);
					$Out = explode("(",$Tab[1],2);
					//$Params = explode(")",$Out[1]);
					$Params[0] = preg_replace("#\)$#","",$Out[1]);
					switch ($Out[0]){
						case "isMail":
							$Params = explode(",",$Params[0]);
							$address = $Params[0];
							if( ereg( ".*<(.+)>", $address, $regs ) ) {
								$address = $regs[1];
							}
							if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int|fr)\$",$address) )
								return true;
							else
								return false;
						break;
						case "isDate":
							$Params = explode(",",$Params[0]);
							$date = $Params[0];
							if( preg_match( "#[0-9]{2}\/[0-9]{2}\/[0-9]{4}#", $date, $regs ) ) {
								return true;
							}else{
								return false;
							}
						break;
						case "isArray":
							$t = $this->processVars($Params[0]);
							return (is_array($t)&&!empty($t));
						break;
						case "getLines":
							$Params = explode(",",$Params[0]);
							$Tab = ereg("^(.*)$",$Params[0],$Out);
							print_r($Out);
							return nl2br($Params[0]);
						break;
						case "nl2br":
							$Params = explode(",",$Params[0]);
							return nl2br($Params[0]);
						break;
						case "noHtml":
							return preg_replace("#<(.*?)>#","",html_entity_decode($Params[0]));
						break;
						case "Clean":
						        $Params = trim($Params[0]);
							$Params = strtolower($Params);
							return $Params;	
							break;
						case "getDate":
							$Params = explode(",",$Params[0]);
							if (!$Params[1]>0)$Params[1]=0;
							return date($Params[0],$Params[1]);
						break;
						case "getTms":
							$P = explode(",",$Params[0]);
							//if (sizeof($P)==1)preg_match("#^([0-9]+?)\/([0-9]+?)\/([0-9]+?)\ ([0-9]+?)\:([0-9]+?)\:([0-9]+?)$#",$Params[0],$P);
							if (sizeof($P)==1)preg_match("#^([0-9]+?)\/([0-9]+?)\/([0-9]+?)\ ([0-9]+?)\:([0-9]+?)$#",$Params[0],$P);
							if (sizeof($P)<=1)preg_match("#^([0-9]+?)\/([0-9]+?)\/([0-9]+?)$#",$Params[0],$P);
							return mktime($P[4],$P[5],$P[6],$P[2],$P[1],$P[3]);
						break;
						case "Random":
							$Params = explode(",",$Params[0]);
							return sprintf("%x",rand($Params[0],$Params[1]));
						break;
						case "getPrice":
							$Params = explode(",",$Params[0]);
							return sprintf('%.2f',floor($Params[0]*100)/100);
						break;
						case "Canonic":
							$Params = explode(",",$Params[0]);
							if (strlen($Params[0])<3)return "";
							$chaine=utf8_decode($Params[0]);
							$chaine=strtr($chaine,utf8_decode("ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ"),"aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn");
							if (strlen($chaine)>3) $chaine = preg_replace("#(e|s|es)$#","",$chaine);
							return utf8_encode($chaine);
						break;
					}
				break;
				Default:
					//Si le denominateur correspond a un objet temporaire (cas du storproc)
					if ($Tab[0]=="") {$Ob = "Object";}else{$Ob=$Tab[0];}
					if (is_object($this->TempVar[$Ob])) {
						//On enleve les parenthese si il y a
						$Function = explode("(",$Tab[1]);
						$Params = explode(")",$Function[1]);
						$Params = $Params[0];
						$Function = $Function[0];
						if (array_key_exists($Tab[1],$this->TempVar[$Ob])) {
							$Suivre=true;
							if (method_exists($this->TempVar[$Ob],"Get"))
							$Data = $this->TempVar[$Ob]->Get($Tab[1]);
							else $Data = $this->TempVar[$Ob]->$Tab[1];
						}elseif(method_exists($this->TempVar[$Ob],$Function)) {
							//Detection de l existence de parenthese
							if ($Params!=""){
								//On verifie si il n y a pas des variables a traduire
								//Alors il y a des parenthese donc des parametres
								$ParamsTemp = explode(",",$Params);
								$Params="";
								$i=0;
								foreach ($ParamsTemp as $K=>$Key) {
									if (sizeof($Tab = explode("[!",$Key))>1){
										$Tab = explode("!]",$Tab[1]);
// 										$Params.="\$this->TempVar[".$Tab[0]."]";;
										$T[$K] = $this->TempVar[$Tab[0]];
										$Params.="\$T[".$K."]";
									}else{
										//$Params.=$Key;
										$T[$K] = $Key;
										$Params.="\$T[".$K."]";
									}
									if ($i<sizeof($ParamsTemp)-1) $Params.=",";
									$i++;
								}
// 								echo "return \$this->TempVar[\$Ob]->$Function($Params);";
								$Data=eval("return \$this->TempVar[\$Ob]->$Function($Params);");
							}else{
								$Data=$this->TempVar[$Ob]->$Function();
							}
						}
					}elseif(is_array($this->TempVar[$Ob])){
						if ($Tab[1]=="Key") {
// 							print_r($Tab);
						}elseif (array_key_exists($Tab[1],$this->TempVar[$Ob])) {
							$Data = $this->TempVar[$Ob][$Tab[1]];
// 							echo $Tab[0]."::".$Tab[1]." - ".$Data."\r\n";
						}else{
							$this->TempVar[$Ob][$Tab[1]] = "";
							$Data=0;
						}

					}else{
						if ($Tab[1]=="Reset"){
							unset($this->TempVar[$Tab[0]]);
							unset($GLOBALS["Systeme"]->GetVars[$Tab[0]]);
							unset($GLOBALS["Systeme"]->PostVars[$Tab[0]]);
						}
						//----------------------------------------------//
						// VARIABLE INEXISTANTE	OU VIDE			//
						//----------------------------------------------//
						//On teste d abord si ce n est pas une variable temporaire
                        if (is_array($this->TempVar[$Tab[0]])){
                            if (sizeof($this->TempVar[$Tab[0]])) {
                                $Data = $this->TempVar[$Tab[0]][$Tab[1]];
                            }else{
                                //Le cas ou la variable est une variable Post
                                $Data = Process::ProcessPostVars($Tab[0]);
                                $Data = $Data[$Tab[1]];
                            }
                        }elseif(is_object($this->TempVar[$Tab[0]])) {
                            $Data = $this->TempVar[$Tab[0]];
                            $n = $Tab[1];
                            $Data = $Data->$n;
                        }elseif(is_object(Process::ProcessPostVars($Tab[0]))) {
                            $Data = Process::ProcessPostVars($Tab[0]);
                            $n = $Tab[1];
                            $Data = $Data->$n;
                        }elseif(is_array(Process::ProcessPostVars($Tab[0]))) {
                            $Data = Process::ProcessPostVars($Tab[0]);
                            $Data = $Data[$Tab[1]];
                        }					}
				break;

			}
			//Si le premier mot est un module, alors on recherche des informations sur un objet de ce module.
		}else {
			//On teste d abord si ce n est pas une variable temporaire
			if (!is_object($this->TempVar[$Data])){
				if (strlen($this->TempVar[$Data])) {
					$Data = $this->TempVar[$Data];
				}else{
					//Le cas ou la variable est une variable Post
					$Data = Process::ProcessPostVars($Data);
				}
			}elseif(is_object($this->TempVar[$Data])) {
				$Data = $this->TempVar[$Data];
			}
		}
		if (!empty($Defaut)){
			$Data= $Defaut;
		}
		if ($Data===$sData){
			$Data="";
		}
		return $Data;
	}
	function Canonise($Params) {
		$chaine=utf8_decode($Params);
		$chaine=strtr($chaine,utf8_decode("ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ"),"aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn");
		if (strlen($chaine)>3) $chaine = preg_replace("#(e|s|es)$#","",$chaine);
		return utf8_encode($chaine);
	}	

	//C est la methode qui scanne la chaine a la recherche de balise varible avant d envoyer a processVars
	function searchAndReplaceVars($Data,$DebBeacon="[!",$FinBeacon="!]") {
		//On ecarte le debut de la chaine jusqu a la balise
 		$TempData = explode($FinBeacon,trim($Data));
		if (sizeof($TempData)>1){
			$EndResult = array_pop($TempData);
			$TempData = implode($FinBeacon,$TempData).$FinBeacon;
		}else{
			$TempData=trim($Data);
		}
		$TempData = explode($DebBeacon,$TempData,2);
		//On conserve le debut
//  		echo "\r\n---SEARCH AND REPLACE-".trim($Data)."-----\r\n";
		$BeginResult = $TempData[0];
		//$BeginResult="";
// 		$Data = $TempData[1];
		$nbFerm = $nbOuv = $nbReplace= 0;
		$TempData = explode($DebBeacon,trim($TempData[1]));
		$Temp="";
		$Result="";
		$Flag=false;
		$l = 0;
		for ($g=0;$g<sizeof($TempData);$g++) {
			$Key = $TempData[$g];
			$nbOuv++;
			//On compte le nombre de fermeture de balise et on compare avec le nombre d ouverture
			$TempData2 = explode($FinBeacon,$Key);
			if (sizeof($TempData2)>1){
				if ($nbOuv>1) $Temp.=$DebBeacon;
				for($x=0;$x<(sizeof($TempData2)-1);$x++) {
					if ($Flag) {
						$TempResult.=$TempData2[$x].$FinBeacon;
					}else{
						//Si le nombre d ouverture est egal au nombre de fermeture
						$nbFerm++;
						if ($nbFerm==$nbOuv){
							//On reconstitue la variable
							$Var = $Temp.$TempData2[$x];
							$Temp=$TempResult="";
							//Remplacement de la variable par sa valeur
// 							echo "--> SEND $Var \r\n";
							$TempResult= $this->processVars($Var,"ZOB");
// 							print_r($TempResult);
							if ((is_object($TempResult)||is_array($TempResult))&&(sizeof($TempData)>$nbOuv+1||$BeginResult!=""||$EndResult!="")){
								if (preg_match("#(.*)(\:\=|\=\=)#",$BeginResult,$Out)) {
									$this->registerTempVar($Out[1],$TempResult);
									return "";
								}else{
									$this->registerTempVar("#".$Var."#",$TempResult);
									$TempResult = $DebBeacon."#".$Var."#".$FinBeacon;
								}
// 								echo "-->$Data<-- ".getType($TempResult)." $Result\r\n";
							}elseif((is_object($TempResult)||is_array($TempResult))&&sizeof($TempData)==$nbOuv+1) {
//  								echo "--> $Data $Var ".$BeginResult." ".getType($TempResult)."<-- \r\n";
//  								print_r($TempResult);
								return $TempResult;
							}

/* 							echo "SEARCH AND REPLACE VARS $Var \r\n";
 							print_r($TempResult);*/
							//On reinitialise les variables
							$nbFerm=$nbOuv=0;
							$Flag =true;
						}else{
							//Sinon on remet les Balises
							$Temp.=$TempData2[$x].$FinBeacon;
						}
					}
				}
				if ($Flag) {
					if ($l==sizeof($TempData)-1) {
						//c le dernier ajout
						//$EndResult=$TempData2[(sizeof($TempData2)-1)];
					}else{
						$TempResult.=$TempData2[(sizeof($TempData2)-1)];
					}
				}else{
					$Temp.=$TempData2[(sizeof($TempData2)-1)];
				}
			}else{
				if ($nbOuv>1) {
					$Temp.=$DebBeacon.$Key;
					$TempOuv = $nbOuv;
					//Si la premiere fois qu il trouve une balise alors on enregistre le debut de la phrase
				}else{ $Temp.=$Key;}
			}
			if ($Flag) {
				$Flag=false;
				if ((is_array($TempResult)||is_object($TempResult))&&sizeof($TempData)<=2&&$BeginResult==""&&$EndResult==""){
					return $TempResult;
				}else{
					if (!is_array($TempResult)&&!is_object($TempResult)) {
						$Result.=$TempResult;
					}else{
						//Le cas de l egalite
						if (sizeof(preg_match("#(.*)(\:\=|\=\=)#",$BeginResult.$Result,$Out))&&$EndResult==""){
							$this->registerTempVar($Out[1],$TempResult);
							return ;
						}else{
							//Donc on stocke la variable dans les variables temporaires
							$this->registerTempVar($Var,$TempResult);
							//$Result.=$DebBeacon.$Var.$FinBeacon;
							$Result=$TempResult;
						}
					}
				}
			}
			//on incremente pour retrouver le dernier enregistrement
			$l++;
		}
		if (is_string($Result)) return (!is_object($Result))?$BeginResult.$Result.$EndResult:$Result;
		if (is_array($Result)) {
			return $Result;
		}
		if (is_object($Result)) return $Result;
	}

	//Cette methode recherche les groupements de variables definies dans un formulaire
	//#DEPRECATED
// 	function processQuery($Prefix) {
// 		$Post= $GLOBALS["Systeme"]->PostVars;
// 		$PostKey = array_keys($Post);
// 		foreach ($PostKey as $Key) {
// 			if (preg_match("#".$Prefix."(.*)#",$Key,$Out)) {
// 				$Result[$Out[1]] = $Post[$Key];
// 			}
// 		}
// 		return $Result;
// 	}

	//Cette methode convertie les variables Registered/GET/POST
	//TODO: Empecher des hacks par la methode POST
	function ProcessPostVars($Data,$Restore=false) {
		$sData=$Data;
		//Test des variables Registered
		$Temp=$this->getRegVars($sData);
		if ($Temp!=NULL) $Data=$Temp;
		$Temp="";
		//Test des variables POSTS
		$Temp=$this->getPostVars($sData);
		if ($Temp!=NULL) $Data=$Temp;
		$Temp="";
		//Test des variables GET
		$Temp=$this->getGetVars($sData);
		if ($Temp!=NULL) $Data=$Temp;
		//Test des variables FILES
		$Temp=$this->getFilesVars($sData);
		if ($Temp!=NULL) $Data=$Temp;
		return $Data;
	}

	//Cette methode recupere la valeur d une variable POST
	function getPostVars($Data) {
		return $GLOBALS["Systeme"]->getPostVars($Data);
	}

	//Cette methode recupere la valeur d une variable GET
	function getGetVars($Data) {
		return $GLOBALS["Systeme"]->getGetVars($Data);
	}

	//Cette methode recupere la valeur d une variable REGISTERED
	function getRegVars($Data) {
		return $GLOBALS["Systeme"]->getRegVars($Data);
	}
	function getFilesVars($Data) {
		return $GLOBALS["Systeme"]->getFilesVars($Data);
	}

// --------------------------------------------------//
//			TRAITEMENT DES BALISES	     //
// --------------------------------------------------//

	//24/04/2007
	//Methode Generique pour generer une balise
	//HEADER TITLE DESCIPTION ... OK
	//BLOC COL LINE MODULE ... OK
	//IF SWITCH ... OK
	//STORPROC LIMIT ORDER ... OK
	//URL SUBSTR ... OK
	//LIB ... OK
	function getBeacon($Var,$Data,$Beacon) {
		$Class = $Beacon["OBJECT"];
		$Obj = new $Class;
// 		print_r($this->Objects);
		$Obj->setFromVar($Var,$Data,$Beacon);
		$this->Objects[] = $Obj;
// 		print_r($this->Objects);
	}

// --------------------------------------------------//
//			PROCESSUS		     //
// --------------------------------------------------//

	//Cette methode place une chaine de caratere dans le conteneur temporaire
	function getVarChar($Data) {
		if (!is_string($Data)){
			$this->Objects[] = $Data;
			return ;
		}
		$Test = $Data;
		$Test = str_replace("	","",$Test);
		$Test = preg_replace("#\s#","",$Data);
		if ($Test!="") {
			$this->Objects[] = $Data;
		}
	}

	//Recherche la premiere balise du tableau dans la chaine
	function searchFirstBeacon($Data,$BeaconTab){
		if (is_string($Data)) {
			foreach ($BeaconTab as $Key){
				$Temp[] = explode("[".$Key["BEACON"],$Data,2);
			}
			$Long = strlen($Data);
			$i=0;
			foreach ($Temp as $Key){
				if (strlen($Key[0])<$Long) {
					$Long = strlen($Key[0]);
					$Result = $i;
				}
				$i++;
			}
			return $Result;
		}else{
			foreach ($Data as $Temp) {
				if (is_string($Temp)) {
					foreach ($BeaconTab as $k=>$Key){
						$TempResult[$k] = explode("[".$Key["BEACON"],$Temp);
					}
					$Long = strlen($Temp);
					$Max = strlen($Temp);
					foreach ($TempResult as $k=>$Key){
						if (strlen($Key[0])<$Long) {
							$Long = strlen($Key[0]);
							$Result = $k;
						}
					}
					if ($Max!=$Long) return $Result;
				}
			}
		}
	}

	//Supprime les balises non utilisees du tableau
	function deleteUnusedBeacon($Data,$BeaconTab) {
		if (is_string($Data)) {
			if (is_array($BeaconTab)) {
				foreach ($BeaconTab as $Key){
					if (sizeof(explode("[".$Key["BEACON"],$Data,2))>1) $Result[]=$Key;
				}
			}
			return $Result;
		}elseif(is_array($Data)){
			if (is_array($BeaconTab)) {
				foreach ($BeaconTab as $Key){
					$Flag=false;
					foreach ($Data as $Temp) {
						if (!is_object($Temp))if (sizeof(explode("[".$Key["BEACON"],$Temp))>1) {
							$Flag=true;
						}
					}
					if ($Flag) $Result[]=$Key;
				}
			}
			return $Result;
		}
	}

	//On separe les attributs de la donnée
	function splitAttributes($Data) {
		if (is_string($Data)) {
			//Recherche du Data et des Attributs
			$TempData = explode("]",$Data);
			$m=0;
			$Attributes="";
			$Donnee="";
			$Flag=false;
			for ($l=0;$l<sizeof($TempData);$l++) {
				if (!$Flag) {
					$m+=substr_count($TempData[$l],"[");
					if (substr($TempData[$l],0,1)==" "){
						//Si le premier caractere est un espace
						$Attributes.=substr($TempData[$l],1,strlen($TempData[$l]));
					}else{
						$Attributes.=$TempData[$l];
					}
					if ($m==$l) {$Flag=true;}else{$Attributes.="]";}
				}else{
					$Donnee.=$TempData[$l];
					if ($l<sizeof($TempData)-1)$Donnee.="]";
				}
			}
			$Result[0] = $Attributes;
			$Result[1] = $Donnee;
			return $Result;
		}else{
			//Recherche du Data et des Attributs
			$TempData = explode("]",$Data[0]);
			$m=0;
			$Attributes="";
			$Donnee="";
			$Flag=false;
			for ($l=0;$l<sizeof($TempData);$l++) {
				if (!$Flag) {
					$m+=substr_count($TempData[$l],"[");
					if (substr($TempData[$l],0,1)==" "){
						//Si le premier caractere est un espace
						$Attributes.=substr($TempData[$l],1,strlen($TempData[$l]));
					}else{
						$Attributes.=$TempData[$l];
					}
					if ($m==$l) {$Flag=true;}else{$Attributes.="]";}
				}else{
					$Donnee.=$TempData[$l];
					if ($l<sizeof($TempData)-1)$Donnee.="]";
				}
			}
			$Result[0] = $Attributes;
			$Data[0] = $Donnee;
			$Result[1] = $Data;
			return $Result;
		}
	}

	//Recherche le contenu de la balise mentionne et la remplace par le retour de sa fonction
	function searchAndReplaceBeacon($Data,$Beacon,$AllBeacon="",$NoPost=false) {
		if (sizeof($Beacon)) {
			if (array_key_exists("FUNCTION",$Beacon)) $Function = $Beacon["FUNCTION"]; else $Function = "getBeacon";
			if (array_key_exists("POST",$Beacon)&&!$NoPost) $Post = $Beacon["POST"];
			if (array_key_exists("VAR",$Beacon)) $Var = $Beacon["VAR"];
			if (array_key_exists("REPLACE",$Beacon)) $Replace = $Beacon["REPLACE"];
		}
		$BeaconTab = $Beacon;
		$Beacon = $Beacon["BEACON"];
		if (empty($AllBeacon)) $NoError=true;
		if (is_string($Data)) {
			//On ecarte le debut de la chaine jusqu a la balise
			$TempData = explode("[".$Beacon,$Data,2);
			//On conserve le debut
			$BeginResult = $TempData[0];
			//On enregistre Le debut en tant que varchar
			$BeginResult=$this->getVarChar($BeginResult);
//			$BeginResult="";
			$Data = $TempData[1];
			$nbFerm = $nbOuv = $nbReplace= 0;
			$TempData = explode("[".$Beacon,$Data);
			$Temp="";
			$Flag=false;
			$l = 0;
			for ($g=0;$g<sizeof($TempData);$g++) {
				$Key = $TempData[$g];
				$nbOuv++;
				//On compte le nombre de fermeture de balise et on compare avec le nombre d ouverture
				$TempData2 = explode("[/".$Beacon."]",$Key);
				if (sizeof($TempData2)>1){
					if ($nbOuv>1) $Temp.="[".$Beacon;
					for($x=0;$x<(sizeof($TempData2)-1);$x++) {
						if ($Flag) {
							$TempResult.=$TempData2[$x]."[/".$Beacon."]";
						}else{
							//Si le nombre d ouverture est egal au nombre de fermeture
							$nbFerm++;
							if ($nbFerm==$nbOuv){
								//On separe les attributs du DATA
								$TempData3 = Process::splitAttributes($Temp.$TempData2[$x]);
								$Temp=$TempResult="";
								$Attributes = $TempData3[0];
								$Donnee = $TempData3[1];
								if ((strlen($Function))&&(!strlen($Replace))) {
									$FuncString = "return \$this->".$Function."(stripslashes('".addslashes($Attributes)."'),stripslashes('".addslashes($Donnee)."')";
									$FuncString.=",\$BeaconTab";
									$FuncString.=");";
									$TempResult.= eval($FuncString);
								}else{
									$TempResult.= "[".$Replace."-".$nbReplace."]";
									$ReplaceResult[$Replace."-".$nbReplace] = $Attributes."]".$Donnee;
									$nbReplace++;
								}
								//On reinitialise les variables
								$nbFerm=$nbOuv=0;
								$Flag =true;

							}else{
								//Sinon on remet les Balises
								$Temp.=$TempData2[$x]."[/".$Beacon."]";
							}
						}
					}
					if ($Flag) {
						if ($l==sizeof($TempData)-1) {
							//c le dernier ajout
							$EndResult=$TempData2[(sizeof($TempData2)-1)];
						}else{
							$TempResult.=$TempData2[(sizeof($TempData2)-1)];
						}
					}else{
						$Temp.=$TempData2[(sizeof($TempData2)-1)];
					}
				}else{
					if ($nbOuv>1) {
						$Temp.="[".$Beacon.$Key;
						$TempOuv = $nbOuv;
						//Si la premiere fois qu il trouve une balise alors on enregistre le debut de la phrase
					}else{ $Temp.=$Key;}
				}
				if ($Flag) {
					$Flag=false;
					$Result.=$TempResult;
					//On teste si la premiere balise suivante est bien la meme


	/*				if ($Beacon=="BLOC"){ echo "-------------------------------------------\r\n";print_r($AllBeacon);print_r($TempData);echo $Result."\r\n";
						echo "-=>".Process::searchFirstBeacon($Result,$AllBeacon)."\r\n";
					}*/
					if (!$NoError) {
	// 					echo "Test->".$Result;
						$TR = preg_replace("#\s#","",$Result);
	/*					$TR = str_replace(" ","",$Result);
						$TR = str_replace("\r","",$TR);
						$TR = str_replace("\n","",$TR);*/
	// 					echo " Resultat->".$TR."\r\n";

						if (strlen(Process::searchFirstBeacon($Result,$AllBeacon))||$TR!="") {
		/*					echo "-=> Test ".$Beacon." - ".Process::searchFirstBeacon($Result,$AllBeacon)."\r\n";
							print_r($TempData);
							echo "-------------------------------------------\r\n";
		*/
							for ($v=$g+1;$v<sizeof($TempData);$v++) $Result.="[".$Beacon.$TempData[$v];
		// 					echo "-------------------\r\n".$Result."\r\n";
							//On sort de la boucle
							$g=sizeof($TempData);
						}/*else{
							//C la meme balise donc on continue par contre on stocke les phrases eventuelles
							$this->getVarChar($Result);
							$Result="";
						}*/
					}
				}
				//on incremente pour retrouver le dernier enregistrement
				$l++;
			}
			//On execute la fonction de Post Processing
			if ($Post!="") {
				$this->execPost($BeaconTab["OBJECT"],$Post,$Result);
			}
			if (strlen($Replace)) {
				echo "<< ! >> PROCESS REPLACE<BR />\r\n";
				//Alors on renvoie un tableau avec le data en 0 et le tableau de remplacement en 1
				$Resultat[0] = $BeginResult.$Result.$EndResult;
				$Resultat[1] = $ReplaceResult;
				return $Resultat;

			}
			return $BeginResult.$Result.$EndResult;
		}else{
			//On reinitialise les variables
// 			echo ">>>------------------ARRAY--------------\r\n";
			$Temp="";
			$Flag=false;
			$nbFerm = $nbOuv = $nbReplace= 0;
			$Begin = false;
			foreach ($Data as $DataTab) {
				if (is_string($DataTab)) {
					if ((sizeof(explode("[".$Beacon,$DataTab,2))>1)||$Begin) {
						$Begin = true;

						if (!sizeof(explode("[/".$Beacon."]",$DataTab))) {
							$TempData = explode("[".$Beacon,$DataTab,2);
							//ON initialise les variables du processus
							$TempData = explode("[".$Beacon,$TempData[1]);
						}else{
							$TempData = explode("[".$Beacon,$DataTab);
						}
						$l = 0;
						//On boucle sur les ouvertures de balise de la chaine en cours
// 						echo ">>> ON A TROUV� $Beacon ".sizeof($TempData)."\r\n";
						for ($g=0;$g<sizeof($TempData);$g++) {
							$Key = $TempData[$g];
							if ($g>0) $nbOuv++;
							//if ($nbOuv==1) $BigResult=Array();
							//On compte le nombre de fermeture de balise et on compare avec le nombre d ouverture
							$TempData2 = explode("[/".$Beacon."]",$Key);
							if (sizeof($TempData2)>1){
								if ($nbOuv>1) $Temp.="[".$Beacon;
								for($x=0;$x<(sizeof($TempData2)-1);$x++) {
									if ($Flag) {
										//Si on est dans la variable de resultat
										$TempResult.=$TempData2[$x]."[/".$Beacon."]";
									}else{
										//Si le nombre d ouverture est egal au nombre de fermeture
										$nbFerm++;
										if ($nbFerm==$nbOuv){
											//C est le debut de la variable resultat
											//On separe les attributs du DATA
											$TempData4 = $BigResult;
											$TempData4[]=$TempData2[$x];
											$TempData3 = Process::splitAttributes($TempData4);
											$Temp=$TempResult="";
											$Attributes = $TempData3[0];
											$Donnee = $TempData3[1];
											$BigResult=$Donnee;
											if ((strlen($Function))&&(!strlen($Replace))) {
												//Le cas d une execution de fonction
												$FuncString = "return \$this->".$Function."(stripslashes('".addslashes($Attributes)."'),\$BigResult";
												$FuncString.=",\$BeaconTab";
												$FuncString.=");";
												$TempResult.= eval($FuncString);
// 												echo ">>> $FuncString\r\n";
											}else{
												//Le cas d un remplacement
												$TempResult.= "[".$Replace."-".$nbReplace."]";
												$ReplaceResult[$Replace."-".$nbReplace] = $Attributes."]".$Donnee;
												$nbReplace++;
											}
											$BigResult="";
											//On reinitialise les variables
											$nbFerm=$nbOuv=0;
											$Flag =true;

										}else{
											//Sinon on remet les Balises
											$Temp.=$TempData2[$x]."[/".$Beacon."]";
										}
									}
								}
								if ($Flag) {
									if ($l==sizeof($TempData)-1) {
										//c le dernier ajout
										$EndResult=$TempData2[(sizeof($TempData2)-1)];
									}else{
										$TempResul.=$TempData2[(sizeof($TempData2)-1)];
									}
								}else{
									$Temp.=$TempData2[(sizeof($TempData2)-1)];
								}
							}else{
								//Il y a des balises d ouverture mais pas de fermeture
								if ($nbOuv>1) {
									//Si on a passe� plus d une ouverture alors on ajoute la balise
									$Temp.="[".$Beacon.$Key;
									$TempOuv = $nbOuv;
								}else{
									//Si c est la premiere alors on enregistre simplement la phrase
									$Temp.=$Key;
								}
							}
							if ($Flag) {
								//Si nous sommes dans une variable resultat
								$Flag=false;
								$Result.=$TempResult;
/*								echo "////////////////////////////////////////\r\n";
								echo $Result;*/
								if (!$NoError) {
									$TR = preg_replace("#\s#","",$Result);
									if (strlen(Process::searchFirstBeacon($Result,$AllBeacon))||$TR!="") {
										for ($v=$g+1;$v<sizeof($TempData);$v++) $Result.="[".$Beacon.$TempData[$v];
										$g=sizeof($TempData);
									}
								}
							}
							//on incremente pour retrouver le dernier enregistrement
							$l++;
						}
						$BigResult[]=$Temp;
						$Temp="";
					}else{
						//Il n y pas encore eu d ouverture de balise
					}
				}else{
					//C un objet donc on le rajoute dasn la variable resultat
					if ($Begin)$BigResult[]=$DataTab;
				}
			}
			return $BigResult;

		}
	}

	function execPost($Obj,$Func,$Data) {
		eval($Obj."::".$Func."(stripslashes('".addslashes($Data)."'),\$this);");
	}

	//Recherche de la premiere balise dans la donnée en entree
	//Ensuite on recherche la balise fermante correspondante
	function processRecursiv($Data,$BeaconTab,$NoPost=false) {
		//Analyse du tableau des balises
		$BeaconTabTemp = Process::deleteUnusedBeacon($Data,$BeaconTab);
//		$BeaconTabTemp = $BeaconTab;
		//Recherche de la premiere balise
		if (sizeof($BeaconTabTemp)) {
			$FirstBeacon = Process::searchFirstBeacon($Data,$BeaconTabTemp);
			//recuperation du contenu et execution des fonctions
			if ($BeaconTabTemp[$FirstBeacon]["TYPE"]!="ORPHAN") {
				$Data = $this->searchAndReplaceBeacon($Data,$BeaconTabTemp[$FirstBeacon],$BeaconTabTemp,$NoPost);
			}else{
// 				echo "RECHERCHE ENFANT ORPHAN ".$BeaconTabTemp[$FirstBeacon]["BEACON"]."\r\n";
				$Data =$this->searchAndReplaceOrphanBeacon($Data,$BeaconTabTemp[$FirstBeacon]);
			}
			//Appel recursif.
			$TempData = $this->processRecursiv($Data,$BeaconTab,$NoPost);
			if ($TempData!=$Data) $Data=$TempData;
		}else{
			//Il n y a aucune balise donc on stocke la phrase restante en VarChar
			$this->getVarChar($Data);
		}
		//retour Data;
		return $Data;
	}

	//Recherche de la premiere balise dans la donnée en entree
	//Ensuite on recherche la balise fermante correspondante
	function processChild($Data,$MotherBeacon,$BeaconTab,$Recursiv=true) {
		//Analyse du tableau des balises
		$MotherBeaconTab["BEACON"] = $MotherBeacon;
//		$MotherBeaconTab["REPLACE"] = "TEMP-".$MotherBeacon;
		$BeaconTab[] = $MotherBeaconTab;
		$BeaconTabTemp = Process::deleteUnusedBeacon($Data,$BeaconTab);
		//Recherche de la premiere balise
		if (sizeof($BeaconTabTemp)) {
			$FirstBeacon = Process::searchFirstBeacon($Data,$BeaconTabTemp);
			//recuperation du contenu et execution des fonctions
			if ($BeaconTabTemp[$FirstBeacon]["BEACON"]==$MotherBeacon) {
				//FIXME
/*				//On stoke la balise dans une variable temporaire
				$DataTemp = Process::searchAndReplaceBeacon($Data,$BeaconTabTemp[$FirstBeacon]);
				$Data = $DataTemp[0];
				$ReplaceTab = $DataTemp[1];
				if (is_array($ReplaceTab)) $ReplaceKey = array_keys($ReplaceTab);
				for ($i=0;$i<sizeof($ReplaceTab);$i++) $Data=preg_replace("#\[".$ReplaceKey[$i]."]#",$ReplaceTab[$ReplaceKey[$i]],$Data);*/
			}else{
				$Data = Process::searchAndReplaceBeacon($Data,$BeaconTabTemp[$FirstBeacon]);
			}
/*			if ($Recursiv) {
				$TempData = Process::processRecursiv($Data,$MotherBeacon,$BeaconTab);
				if ($TempData!=$Data) $Data=$TempData;
			}*/
		}
		//retour Data;
		return $Data;
	}

	//Recherche de la premiere balise appartenant a la balise mere
	function processChildOrphanBeacon($Data,$MotherBeacon,$BeaconTab) {
		//Analyse du tableau des balises
		$MotherBeaconTab["BEACON"] = $MotherBeacon;
		$MotherBeaconTab["REPLACE"] = "TEMP-".$MotherBeacon;
		$BeaconTabTemp[]=$MotherBeaconTab;
		$BeaconTabTemp = Process::deleteUnusedBeacon($Data,$BeaconTabTemp);
		//Recherche de la premiere balise
		if (sizeof($BeaconTabTemp)) {
			$FirstBeacon = Process::searchFirstBeacon($Data,$BeaconTabTemp);
			//recuperation du contenu et execution des fonctions
			//On stoke la balise dans une variable temporaire
			$DataTemp = Process::searchAndReplaceBeacon($Data,$BeaconTabTemp[$FirstBeacon]);
			$Data = $DataTemp[0];
			$ReplaceTab = $DataTemp[1];
		}
		//Maintenant on execute les fonctions
		$Data = Process::searchAndReplaceOrphanBeacon($Data,$BeaconTab);
		if (sizeof($BeaconTabTemp)) {
			//Ensuite on remplace les balises Temporaires
			if (is_array($ReplaceTab)) $ReplaceKey = array_keys($ReplaceTab);
			for ($i=0;$i<sizeof($ReplaceTab);$i++) $Data=preg_replace("#\[".$ReplaceKey[$i]."\]#","[".$MotherBeacon." ".$ReplaceTab[$ReplaceKey[$i]]."[/".$MotherBeacon."]",$Data);
		}

		//retour Data;
		return $Data;
	}


	//Recherche des balises orphelines et remplacement par function.
	function searchAndReplaceOrphanBeacon($Data,$Beacon) {
		$TempData = $Data;
		$Result="";
		$Temp = explode("[".$Beacon["BEACON"],$TempData,2);
		if (sizeof($Temp)>1) {
			$this->getVarChar($Temp[0]);
			for ($i=1;$i<sizeof($Temp);$i++) {
				$TempResult = Process::splitAttributes($Temp[$i]);
				$this->getBeacon($TempResult[0],false,$Beacon);
				//On complete le resultat
				$Result.=$TempResult[1];
			}
		}
		return $Result;
	}


	//Recherche de la premiere balise dans la donnée en entree
	//Ensuite on recherche la balise fermante correspondante
	function processSplit($Data,$MotherBeacon,$Beacon) {
		if (is_string($Data)) {
			//Analyse du tableau des balises
			$BeaconTab[] = $Beacon;
			$BeaconTabTemp = Process::deleteUnusedBeacon($Data,$BeaconTab);
			if (sizeof($BeaconTabTemp)) {
				//On divise le data
				$TempData = explode("[".$Beacon."]",$Data);
				//ensuite on parcoure le tableau pour rechercher les ouvertures/fermeture de la balise mere
				$i=$nbOuv=$nbFerm=$j=0;
				foreach ($TempData as $Key){
					//On compte les ouvertures
					$nbOuv += substr_count($Key,"[".$MotherBeacon);
					//On compte les fermetures
					$nbFerm += substr_count($Key,"[/".$MotherBeacon."]");
					if ($nbOuv==$nbFerm) {
						$Result[$i].=$Key;
						$i++;
					}else{
						$nbOuv=$nbFerm=0;
						//Sinon c pas le bon encore donc on ajoute la chaine
						$Result[$i].=$Key;
						if ($j<sizeof($TempData)-1) $Result[$i].="[".$Beacon."]";
					}
					$j++;
				}

			}
			//retour Data;
			return $Result;
		}elseif(is_array($Data)) {
			//Analyse du tableau des balises
			$BeaconTab[] = $Beacon;
			$BeaconTabTemp = Process::deleteUnusedBeacon($Data,$BeaconTab);
			if (!empty($BeaconTabTemp)) {
				//On divise le data
				if (!empty($Data)) {
					$i=0;
					foreach ($Data as $Temp) {
						if (is_string($Temp)) {
							$TempData = explode("[".$Beacon."]",$Temp);
							//ensuite on parcoure le tableau pour rechercher les ouvertures/fermeture de la balise mere
							if (sizeof($TempData)>1) {
								$Result[$i][]=$TempData[0];
								$i=1;
								$Result[$i][]=$TempData[1];
							}else{
								$Result[$i][]=$Temp;
							}
						}else{
							$Result[$i][]=$Temp;
						}
					}
				}
			}else{
				$Result[0]=$Data;
			}
			//retour Data;
			return $Result;
		}
	}

	//Remplacement de la balise DATA
	function ProcessData($Data,$Contenu){
		return preg_replace('#\[DATA\]#',$Contenu , $Data);
	}

	function processRequire($Data) {
		if (is_string($Data)) {
			//Recuperation d un fichier du cache
			preg_match("#\[\=(.*)\=\]#",$Data,$Out);
			if (!file_exists($Out[1])) {
				return $Data;
			}else{
				return unserialize(file_get_contents($Out[1]));
			}
		}elseif (is_array($Data)) {
			for($i=0;$i<sizeof($Data);$i++) {
				if (is_string($Data[$i])) {
					//Recuperation d un fichier du cache
					preg_match("#\[\=(.*)\=\]#",$Data[$i],$Out);
					if (file_exists($Out[1])) {
						$Data[$i]=unserialize(file_get_contents($Out[1]));
					}
				}
			}
		}
		return $Data;
	}

	function processModule($Data) {
		$BaliseTemp["BEACON"] = "MODULE";
		$BaliseTemp["OBJECT"] = "Bloc";
		$BaliseTemp["TYPE"] = "ORPHAN";
		$Balise[]=$BaliseTemp;
		$Data = $this->searchAndReplaceOrphanBeacon($Data,$Balise);
		$Balise="";
		return $Data;
	}

	function getContent($TabObj) {
		$Data="";
		if (sizeof($TabObj)) {
			for ($i=0;$i<count($TabObj);$i++) {
				if (is_object($TabObj[$i])  && is_a($TabObj[$i],"Beacon")) {
					$TabObj[$i]->init($this);
					$Data .= $TabObj[$i]->Affich();
				}elseif (!is_object($TabObj[$i])){
					if (is_string($TabObj[$i])) $Data .= $TabObj[$i];
				}

			}
		}
		return $Data;
	}

	function PostProcessing($Data) {
//		echo "Post";
		//On supprime les commentaires
		$this->Objects=Array();
/*		echo $Data."\r\n";
		echo "///////////////////////////////////////////////////////\r\n";
		echo $Data."\r\n";*/
		$Data=$this->processRecursiv($Data,$this->PostBeacons,true,false);
// 		print_r($this->Objects);
		$Data=$this->getContent($this->Objects);
/*		echo $Data."\r\n";
		echo "-------------------------------------------------------\r\n";*/
		return $Data;
	}

	function Processing($Data,$NoPost=false,$Tab=Array()) {
		//O supprime les commentaires
 		$Data = preg_replace("#(?<!http:)(?<!https:)//(.*)#m","",$Data);
		//*************************************************************
		//DEBUG
/*		echo "---------------------DEPART PROCESSING------------\r\n";
		echo "DATA :\r\n".$Data."\r\n";
		echo "TAB :\r\n";
		print_r($Tab);
		echo "////PROCESSING////\r\n";*/
		//DEBUG
		//*************************************************************
		$this->Objects = $Tab;
		$Data=$this->processRecursiv($Data,$this->Beacons,$NoPost);
/*		echo "RESULTAT :\r\n";
		print_r($this->Objects);
		echo "---------------------FIN PROCESSING------------\r\n";*/
		if (is_array($this->Objects)) foreach ($this->Objects as $K=>$D) {
			if (is_object($D)){
				$D->init($this);
				$Result[$K] = $D;
			}else $Result[$K] = $D;
		}
		$this->Objects = "";
// 		echo "RETOUR PROCESS >>>>> \r\n";
// 		print_r($Result);
		return $Result;
	}
}

?>
